package com.net.tkc.pro.controller;


import com.net.tkc.pro.entity.Avatar;
import com.net.tkc.pro.entity.Follow;
import com.net.tkc.pro.entity.Good;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.impl.FollowImpl;
import com.net.tkc.pro.impl.GoodImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Follow/")
public class FollowController {
    ResultEntity resultEntity = null;
    @Resource
    FollowImpl followImpl;

    /**
     * 被关注的人查看谁关注了自己
     * @param userfrom 被关注人的id
     * @return
     */
    @GetMapping("GetTo")
    private String getToList(
            @RequestParam("userfrom") int userfrom
    ){
        resultEntity = new ResultEntity();
        try{
            List<Follow> list=followImpl.getToList(userfrom);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("没有关注");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据用户id获取被关注的人的列表
     * @param userto 关注的人id
     * @return
     */
    @GetMapping("GetFrom")
    private String getFromList(
            @RequestParam("userto") int userto
    ){
        resultEntity = new ResultEntity();
        try{
            List<Follow> list=followImpl.getFromList(userto);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("没有粉丝");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 取消关注
     * @param follow 状态
     * @param id id
     * @return
     */
    @PutMapping("Follow")
    private String UpFollow(
            @RequestParam("follow") int follow,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = followImpl.upFollow(follow,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("取消关注成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("取消关注失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 添加关注记录
     * @param userfrom 被关注的人
     * @param userto 关注的人
     * @return
     */
    @PostMapping("Follow")
    private String addFollow(
            @RequestParam("userfrom") int userfrom,
            @RequestParam("userto") int userto
    ){
        resultEntity = new ResultEntity();
        Follow follow=new Follow();
        follow.setUserfrom(userfrom);
        follow.setUserto(userto);
        try{
            int result = followImpl.addFollow(follow);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 根据用户id获取被关注的人还有头像的列表
     * @param userto 关注的人id
     * @return
     */
    @GetMapping("GetFrom1")
    private String getFromList1(
            @RequestParam("userto") int userto
    ){
        resultEntity = new ResultEntity();
        try{
            List<Avatar> list=followImpl.getFromList1(userto);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

}
